package vim.meet.jdkproxy;

import cn.freemethod.bean.DoSomething;
import cn.freemethod.bean.DoSomethingImpl;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;

public class JDKProxy implements InvocationHandler {
    
    private Object target;
    
    public JDKProxy(Object target){
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object result = method.invoke(target, args);
        System.out.println(Arrays.toString(args));
        return result;
    }
    
    public static void main(String[] args) {
        DoSomethingImpl doSomethingImpl = new DoSomethingImpl();
        ClassLoader loader = doSomethingImpl.getClass().getClassLoader();
        Class<?>[] interfaces = {DoSomething.class};
        JDKProxy h = new JDKProxy(doSomethingImpl);
        DoSomething dos = (DoSomething) Proxy.newProxyInstance(loader, interfaces, h);
        System.out.println(dos.doSomething("proxy"));
    }

}
